﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace COMP376A3_9609695
{
    class Track : Drawable, Updates
    {
        private Model trackModel;
        private Matrix trackModelMat;
        private ModelMesh leftMesh;
        private ModelMesh rightMesh;
        private Vector3 leftColor, rightColor;

        public Track(Model model)
        {
            trackModel = model;
            trackModelMat = Matrix.CreateTranslation(-1 * trackModel.Meshes.ElementAt(0).BoundingSphere.Center);
            trackModelMat *= Matrix.CreateTranslation(-1 * trackModel.Meshes.ElementAt(1).BoundingSphere.Center * Vector3.UnitX);
            trackModelMat *= Matrix.CreateTranslation(0f, -0.6f, 0.0f);
            trackModelMat *= Matrix.CreateScale(2, 1, 10.0f);
            leftMesh = trackModel.Meshes.ElementAt(0);
            rightMesh = trackModel.Meshes.ElementAt(1);
            leftColor = new Vector3(1, 0, 0);
            rightColor = new Vector3(1, 0, 0);
        }

        public void Update(GameTime time)
        {
            KeyboardState keyState = Keyboard.GetState();

            leftColor = new Vector3(0.4f, 0.4f, 0.4f);
            if (keyState.IsKeyDown(Keys.Left))
            {
                leftColor = new Vector3(0.4f, 0.4f, 0.8f);

            }
            foreach (BasicEffect effect in leftMesh.Effects)
            {
                effect.DiffuseColor = leftColor;
            }

            rightColor = new Vector3(0.4f, 0.4f, 0.4f);
            if (keyState.IsKeyDown(Keys.Right))
            {
                rightColor = new Vector3(0.4f, 0.4f, 0.8f);
            }
            foreach (BasicEffect effect in rightMesh.Effects)
            {
                effect.DiffuseColor = rightColor;
            }
        }

        public Model getModel()
        {
            return trackModel;
        }

        public Matrix getModelMatrix()
        {
            return trackModelMat;
        }
    }
}
